﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using ZLCMS.Business;
using Newtonsoft.Json;
using ZLCMS.Data.Model;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.Util;
using NHibernate.Criterion;

namespace ZLCMS.Web.SystemManage.Basics.MealCategory
{
    public partial class MealCategoryInterface : System.Web.UI.Page
    {
        public string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();
            //判断操作类型
            string opt = Request["opt"];
            if (opt == "load_data")
            {
                LoadData();
            }
            else if (opt == "update")
            {
                Update();
            }
            else if (opt == "update_ex")
            {
                UpdateEx();
            }
            else if (opt == "add")
            {
                Add();
            }
            else if (opt == "remove")
            {
                Remove();
            }
        }

        #region 加载产品类型列表 + public void LoadData()
        /// <summary>
        /// 加载产品类型列表
        /// </summary>
        public void LoadData()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                IList<ShopCategory> list = session.CreateCriteria<ShopCategory>().Add(Expression.Eq("OwnerAccount", _userAccount)).Add(Expression.Eq("Kind", "restaurant")).AddOrder(Order.Asc("Sort")).List<ShopCategory>();
               
                Response.Write(JsonConvert.SerializeObject(list));
                Response.End();
            }
        }
        #endregion

        #region 添加 + public void Add()
        /// <summary>
        /// 添加
        /// </summary>
        public void Add()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ShopCategory item = new ShopCategory();
                item.CategoryName = Request["CategoryName"];

                int parentId = 0;
                Int32.TryParse(Request["ParentId"], out parentId);

                ShopCategory parentCate;
                if (parentId == 0)
                {
                    parentCate = new ShopCategory() { Prefix = "_0_" };
                }
                else
                {
                    parentCate = session.Get<ShopCategory>(parentId);
                }

                item.ParentId = parentId;
                item.OwnerAccount = _userAccount;
                item.IsActive = "1";
                item.Sort = 99;
                item.Kind = "restaurant";
                session.Save(item);

                item.Prefix = parentCate.Prefix + item.Id + "_";

                session.Update(item);
                session.Flush();

                Response.Write(JsonConvert.SerializeObject(item));
                Response.End();
            }
        }
        #endregion

        #region 更新 + public void Update()
        /// <summary>
        /// 更新
        /// </summary>
        public void Update()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ShopCategory item = session.Get<ShopCategory>(Int32.Parse(Request["Id"]));
                item.CategoryName = Request["CategoryName"];
                session.Update(item);
                session.Flush();

                Response.Write(JsonConvert.SerializeObject(item));
                Response.End();
            }
        }
        #endregion

        #region 更新类别详细设置 + public void UpdateEx()
        /// <summary>
        /// 更新类别详细设置
        /// </summary>
        public void UpdateEx()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ShopCategory item = session.Get<ShopCategory>(Int32.Parse(Request["Id"]));
                item.Sort = Int32.Parse(Request["Sort"]);
                item.IsActive = Request["IsActive"];
                item.IsHot = Request["IsHot"];
                session.Update(item);
                session.Flush();

                Response.Write(JsonConvert.SerializeObject(item));
                Response.End();
            }
        }
        #endregion

        #region 获取子节点 + public void GetChildNodeIds(IList<ShopCategory> list, int id, ref List<int> ids)
        /// <summary>
        /// 获取子节点ID
        /// </summary>
        /// <param name="list"></param>
        /// <param name="id"></param>
        /// <param name="ids"></param>
        public void GetChildNodeIds(IList<ShopCategory> list, int id, ref List<int> ids)
        {
            foreach (ShopCategory type in list)
            {
                if (type.Id == id)
                {
                    ids.Add(type.Id);
                }
                if (type.ParentId == id)
                {
                    GetChildNodeIds(list, type.Id, ref ids);
                }
            }
        }
        #endregion

        #region 删除 + protected void Remove()
        /// <summary>
        /// 删除
        /// </summary>
        protected void Remove()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                try
                {
                    int nodeId = Int32.Parse(Request["Id"]);

                IList<ShopCategory> list = session.CreateQuery("from ShopCategory where OwnerAccount=:acc")
                    .SetParameter("acc", _userAccount)
                    .List<ShopCategory>();
                List<int> ids = new List<int>();
                GetChildNodeIds(list, nodeId, ref ids);

                session.CreateQuery("delete from ShopCategory where Id in (:ids)")
                    .SetParameterList("ids", ids)
                    .ExecuteUpdate();

                session.Flush();
                Response.Write("ok");
                Response.End();

                }
                catch (Exception ex)
                {
                    Response.Write("查询出错!"+ex.Message);
                    Response.End();
                }
               
            }
        }
        #endregion

    }
}